package com.cykj.pos.controller;

import com.cykj.pos.service.TransactionService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Properties;

/**
 * @author SunJinDuo
 * @date 2022/7/19
 */
@RestController
@Api(tags = "流水")
//@EnableScheduling
public class TransactionController {

    private static final Logger logger = LoggerFactory.getLogger(TransactionController.class);
    @Value("${cykj.path}")
    private String path;
    @Autowired
    private TransactionService transactionService;

//    @Scheduled(fixedDelay = 500)
//    @Scheduled(fixedDelay = 5000)
    public void statistics() throws ParseException, IOException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = df.format(System.currentTimeMillis());
//        logger.info("<<<<<--- 开始时间：" + startTime + "。开启流水统计接口定时任务 --->>>>> -");
        Properties properties = this.getMyProperty();
        if (properties != null) {
            if (("1").equals(properties.getProperty("type"))) {
                transactionService.statistics();
//                transactionService.test();
            }
        }
        String endTime = df.format(System.currentTimeMillis());
//        logger.info("<<<<<--- 结束时间：" + endTime + "。立刻执行下次流水统计接口定时任务 --->>>>> -");
    }

    @PostMapping("/test/bc")
    public void bc(String ids) throws ParseException {
        for (String id: ids.split(",")){
            transactionService.statistic(Long.parseLong(id));
        }
    }
    /**
     * @author: haoran
     * @date: 2022/8/18
     * 方法名称： 读取配置文件，是否启动定时器内的方法
     */
    public Properties getMyProperty() throws IOException {
//        InputStream in = new BufferedInputStream(new FileInputStream("/var/cms.properties"));
        InputStream in = new BufferedInputStream(new FileInputStream(path+"/cms.properties"));
        Properties p = new Properties();
        p.load(in);
        return p;
    }
}
